package personal.web.mvc.controller.accounting;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import personal.exception.UnauthorizedAccessException;
import personal.service.accounting.AccountService;
import personal.service.accounting.ExternalEntityService;
import personal.service.accounting.IncomeExpenseService;
import personal.web.mvc.ControllerExceptionHandler;
import personal.web.mvc.controller.AbstractCommandController;
import personal.web.mvc.io.accounting.AccountingRequestParam;


@Controller()
public class MiscellaneousController extends AbstractCommandController{
	private ControllerExceptionHandler controllerExceptionHandler;
	private AccountService accountService;
	private ExternalEntityService externalEntityService;
	private IncomeExpenseService incomeExpenseService;
	
	public AccountService getAccountService() {
		return accountService;
	}

	@Autowired(required=false)
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public ExternalEntityService getExternalEntityService() {
		return externalEntityService;
	}

	@Autowired(required=false)
	public void setExternalEntityService(ExternalEntityService externalEntityService) {
		this.externalEntityService = externalEntityService;
	}

	public IncomeExpenseService getIncomeExpenseService() {
		return incomeExpenseService;
	}

	@Autowired(required=false)
	public void setIncomeExpenseService(IncomeExpenseService incomeExpenseService) {
		this.incomeExpenseService = incomeExpenseService;
	}

	public ControllerExceptionHandler getControllerExceptionHandler() {
		return controllerExceptionHandler;
	}

	@Autowired(required=false)
	public void setControllerExceptionHandler(ControllerExceptionHandler controllerExceptionHandler) {
		this.controllerExceptionHandler = controllerExceptionHandler;
	}
	
	@ExceptionHandler(RuntimeException.class)
	public void handleException(final RuntimeException e, final HttpServletResponse response) {
		controllerExceptionHandler.handleRuntimeException(e, response);
	}
	
	@ExceptionHandler(UnauthorizedAccessException.class)
	public void handleException(final UnauthorizedAccessException e, final HttpServletResponse response) {
		controllerExceptionHandler.handleRuntimeException(e, response);
	}
	
	@RequestMapping(value="/loadmasterdata",method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> loadMasterData(@RequestBody AccountingRequestParam param, HttpServletResponse httpResponse) {
		Map<String, Object> response = getResponseMap();
		response.put("accounts", accountService.getAllAccounts());
		response.put("accountTypes", accountService.getAllAccountTypes());
		response.put("externalEntities", externalEntityService.getAllExternalEntities());
		response.put("incomeExpenseCategories", incomeExpenseService.getAllIncomeExpenseCategories());
		return response;
	}
	
	@RequestMapping(value="/loadmonthendbals",method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> loadMonthendBalances(@RequestBody AccountingRequestParam param, HttpServletResponse httpResponse) {
		Map<String, Object> response = getResponseMap();
		response.put("balances", accountService.getMonthendAccountBalancesForYear(param.getYear()));
		return response;
	}
	
	@RequestMapping(value="/loadmonthlyincomeexpense",method=RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> loadMonthlyIncomeExpenseTotals(@RequestBody AccountingRequestParam param, HttpServletResponse httpResponse) {
		Map<String, Object> response = getResponseMap();
		response.put("totals", incomeExpenseService.getMonthlyCategoryTotalsForYear(param.getYear()));
		return response;
	}
}
